package com.itheima.edu.info.manager.dao;


import com.itheima.edu.info.manager.domain.Person;

import java.sql.*;
import java.util.ArrayList;

public class Dao {
    //ArrayList<Person> person = new ArrayList<>();
    //Person[] person = new Person[5];
    public String flag;
    public Dao(){

    }
    public Person[] findPerson() {
        ArrayList<Person> person = new ArrayList<>();
        //其实这里没用了,我是在数据库取值
        //我要做的封装成特定类型的集合,在转成数组返回
        //其实任何类型都无所谓,只要我拿到的值是我想要的就行
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://192.168.21.130:3306/studyjava","root","123456Qwe~");
            //3.定义sql
            String sql = "select * from "+ flag;
            //4.获取执行sql的对象
            stmt = conn.createStatement();
            //5.执行sql
            rs = stmt.executeQuery(sql);
            //6.遍历结果集，封装对象，装载集合
            Person per = null;
            while(rs.next()){
                //获取数据
                String id = rs.getString("id");
                String name1 = rs.getString("name1");
                String age = rs.getString("age");
                String birthday = rs.getString("birthday");

                // 创建emp对象,并赋值
                per = new Person();
                per.setId(id);
                per.setName(name1);
                per.setAge(age);
                per.setBirthday(birthday);

                //装载集合
                person.add(per);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
       Person[] per =person.toArray(new Person[person .size()]);
        return per;
    }

    public void addPerson(Person per) {
        ArrayList<Person> person = new ArrayList<>();
        String id = per.getId();
        String name =per.getName();
        String age = per.getAge();
        String birthday = per.getBirthday();
        Statement stmt = null;
        Connection conn = null;
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 定义sql
            String sql = "insert into "+flag+" values("+"'"+id+"'"+","+"'"+name+"'"+","+"'"+age+"'"+","+"'"+birthday+"'"+")";
            //3.获取Connection对象
            conn = DriverManager.getConnection("jdbc:mysql://192.168.21.130:3306/studyjava?characterEncoding=UTF-8","root","123456Qwe~");
            //4.获取执行sql的对象 Statement
            stmt = conn.createStatement();
            //5.执行sql
            int count = stmt.executeUpdate(sql);//影响的行数
            //6.处理结果
           /* System.out.println(count);
            if(count > 0){
                System.out.println("添加成功！");
            }else{
                System.out.println("添加失败！");
            }*/

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //stmt.close();
            //7. 释放资源
            //避免空指针异常
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

       /* int index= -1;
        //        for (int i = 0; i < person .size(); i++) {
        //            if (person .get(i)==null){
        //                index = i;
        //                break;
        //            }
        //        }*/
        //        //if (index!=-1) {
            //person .add(per);
       // }
    }

    public void deletePerson(String id) {
/*        ArrayList<Person> person = new ArrayList<>();
        for (int i = 0; i < person .size(); i++) {
            if (person .get(i)!=null&&person .get(i).getId().equals(id)){
                person .remove(i);
                break;
            }
        }*/
        Connection conn = null;
        Statement stmt = null;
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql://192.168.21.130:3306/studyjava?characterEncoding=UTF-8","root","123456Qwe~");
            //3.定义sql
            String sql  = "delete from "+flag+" where id = '"+id+"'";
            //4.获取执行sql对象
            stmt = conn.createStatement();
            //5.执行sql
            int count = stmt.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.释放资源

            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void updataPerson(String id, Person per) {
       /* ArrayList<Person> person = new ArrayList<>();
        int index = -1;
        for (int i = 0; i < person .size(); i++) {
            if (person .get(i) != null && person .get(i) .getId().equals(id)) {
                index = i;
                break;
            }
        }
        if (index!=-1) {
            person .set(index,per);
        }*/
        String name =per.getName();
        String age = per.getAge();
        String birthday = per.getBirthday();
        Connection conn = null;
        Statement stmt = null;
        try {
            //1. 注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql://192.168.21.130:3306/studyjava?characterEncoding=UTF-8","root","123456Qwe~");
            //3.定义sql
            String sql  = "update "+flag+" set name1 ='"+name+"',age ="+ "'"+age+"',birthday ="+"'"+birthday +"'where id = '"+id+"'";
            //4.获取执行sql对象
            stmt = conn.createStatement();
            //5.执行sql
            int count = stmt.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.释放资源

            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
